package com.example.springboot_manage.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.example.springboot_manage.model.ShoeCategory;
import com.example.springboot_manage.model.ShoeInfo;
import com.example.springboot_manage.repository.AccountRepository;
import com.example.springboot_manage.repository.CategoryRepository;
import com.example.springboot_manage.repository.ShoeInfoRepository;
import com.example.springboot_manage.service.ManageService;
import com.example.springboot_manage.util.JsonUtil;
import com.example.springboot_manage.util.ResultUtil;
import com.example.springboot_manage.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.xml.transform.sax.SAXResult;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ManageServiceImpl implements ManageService {

    @Autowired
    private ShoeInfoRepository shoeInfoRepository;
    @Autowired
    private CategoryRepository categoryRepository;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Value("shoe.list.key")
    private String shoeListKey;
    @Value("category.list.key")
    private String categoryListKey;


    @Override
    public List<ShoeInfo> findBySearchId(int startIndex, int pageSize, String shoeId, String shoeName, String shoeDescription) {
        Map<String,Object> map = new HashMap();
        map.put("startIndex",startIndex);
        map.put("pageSize",pageSize);
        map.put("shoeId",shoeId==""? 0:Integer.parseInt(shoeId));
        map.put("shoeName",shoeName);
        map.put("shoeDescription",shoeDescription);

        return shoeInfoRepository.findBySearchId(map);
    }

    @Override
    public int queryCountById(String shoeId, String shoeName, String shoeDescription) {
        Map<String,Object> map = new HashMap();
        map.put("shoeId",shoeId==""? 0:Integer.parseInt(shoeId));
        map.put("shoeName",shoeName);
        map.put("shoeDescription",shoeDescription);

        return shoeInfoRepository.queryCountById(map);
    }

    @Autowired
    public AccountRepository accountRepository;

    @Override
    public Map updatePassword(String oldPassword, String password, String repassword,int id) {
        Map<String,Object> map = new HashMap();
        String oldPwd = accountRepository.findPassword(id);
        if (oldPwd.equals(oldPassword)){
            if (!password.equals(repassword)){
                map.put("code",1);
                map.put("msg","两次密码不一致！");
            }else{
                if (password.equals(oldPwd)){
                    map.put("code",1);
                    map.put("msg","不能输入最近的密码！");
                    return map;
                }
                Map<String,Object> map1 = new HashMap();
                map1.put("id",id);
                map1.put("password",password);
                int flag = accountRepository.updatePassword(map1);
                map.put("code",flag==1? 0:1);
                map.put("msg",flag==1? "成功":"失败");
            }
        }else{
            map.put("code",1);
            map.put("msg","密码错误！");
        }
        return map;
    }

    @Override
    public Map<String,Object> findShoeInfo(String page, String limit) {
        Map<String,Object> map = new HashMap<>();
        //Redis缓存操作
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        //查看缓存中是否有，没有则查询数据库
        String shoeListJson = valueOperations.get(shoeListKey);
        if (!StringUtils.isEmpty(shoeListJson)){
            List<ShoeInfo> shoeInfos = JsonUtil.jsonToList(shoeListJson,ShoeInfo.class);
            map.put("lists",shoeInfos);
            map.put("count",shoeInfos.size());
            return map;
        }
        int pageSize = Integer.parseInt(limit);
        int startIndex = (Integer.parseInt(page) - 1) * pageSize;
        List<ShoeInfo> shoeInfos = shoeInfoRepository.findAll(startIndex,pageSize);
        int count = shoeInfoRepository.queryAllCount();
        map.put("lists",shoeInfos);
        map.put("count",count);
        valueOperations.set(shoeListKey, JsonUtil.object2JsonStr(shoeInfos));
        return map;
    }

    @Override
    public Map<String, Object> findCategory() {
        Map<String,Object> map = new HashMap<>();
        //Redis缓存操作
        ValueOperations<String, String> valueOperations = redisTemplate.opsForValue();
        //查看缓存中是否有，没有则查询数据库
        String categoryListJson = valueOperations.get(categoryListKey);
        if (!StringUtils.isEmpty(categoryListJson)){
            List<ShoeCategory> categories = JsonUtil.jsonToList(categoryListJson,ShoeCategory.class) ;
            map.put("lists",categories);
            map.put("count",categories.size());
            System.out.println("==================redis+++++++++++++++");
            return map;
        }
        List<ShoeCategory> categories = categoryRepository.findCategory();
        int count = categoryRepository.queryAllCount();
        map.put("lists",categories);
        map.put("count",count);
        valueOperations.set(categoryListKey, JsonUtil.object2JsonStr(categories) );
        return map;
    }


}
